﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EffiProz.Core.Lib
{
    public class HashMappedList : HashMap
    {
        public HashMappedList()
            : this(8)
        {

        }

        public HashMappedList(int initialCapacity)
            : base(initialCapacity)
        {


            isList = true;
        }

        public Object get(int index)
        {

            checkRange(index);

            return objectValueTable[index];
        }

        public override  Object remove(Object key)
        {

            int lookup = getLookup(key, key.GetHashCode());

            if (lookup < 0)
            {
                return null;
            }

            Object returnValue = base.remove(key);

            removeRow(lookup);

            return returnValue;
        }

        public Object remove(int index)
        {

            checkRange(index);

            return remove(objectKeyTable[index]);
        }

        public bool add(Object key, Object value)
        {

            int lookup = getLookup(key, key.GetHashCode());

            if (lookup >= 0)
            {
                return false;
            }

            base.put(key, value);

            return true;
        }

        public override Object put(Object key, Object value)
        {
            return base.put(key, value);
        }

        public Object set(int index,
                          Object value)
        {

            checkRange(index);

            Object returnValue = objectKeyTable[index];

            objectKeyTable[index] = value;

            return returnValue;
        }

        public bool insert(int index, Object key,
                              Object value)
        {

            if (index < 0 || index > size())
            {
                throw new IndexOutOfRangeException();
            }

            int lookup = getLookup(key, key.GetHashCode());

            if (lookup >= 0)
            {
                return false;
            }

            if (index == size())
            {
                return add(key, value);
            }

            HashMappedList hm = new HashMappedList(size());

            for (int i = index; i < size(); i++)
            {
                hm.add(getKey(i), get(i));
            }

            for (int i = size() - 1; i >= index; i--)
            {
                remove(i);
            }

            for (int i = 0; i < hm.size(); i++)
            {
                add(hm.getKey(i), hm.get(i));
            }

            return true;
        }

        public bool set(int index, Object key,
                           Object value)
        {

            checkRange(index);

            if (keySet().contains(key) && getIndex(key) != index)
            {
                return false;
            }

            base.remove(objectKeyTable[index]);
            base.put(key, value);

            return true;
        }

        public bool setKey(int index,
                              Object key)
        {

            checkRange(index);

            Object value = objectValueTable[index];

            return set(index, key, value);
        }

        public bool setValue(int index,
                                Object value)
        {

            bool result;
            Object existing = objectValueTable[index];

            if (value == null)
            {
                result = value != existing;
            }
            else
            {
                result = !value.Equals(existing);
            }

            objectValueTable[index] = value;

            return result;
        }

        public Object getKey(int index)
        {

            checkRange(index);

            return objectKeyTable[index];
        }

        public int getIndex(Object key)
        {
            return getLookup(key, key.GetHashCode());
        }

        public Object[] toValuesArray(Object[] a)
        {

            int _size = size();

            if (a == null || a.Length < _size)
            {
                a = new Object[_size];
            }

            for (int i = 0; i < _size; i++)
            {
                a[i] = base.objectValueTable[i];
            }

            return a;
        }

        public Object[] toKeysArray(Object[] a)
        {

            int _size = size();

            if (a == null || a.Length < _size)
            {
                a = new Object[_size];
            }

            for (int i = 0; i < _size; i++)
            {
                a[i] = base.objectKeyTable[i];
            }

            return a;
        }

        private void checkRange(int i)
        {

            if (i < 0 || i >= size())
            {
                throw new IndexOutOfRangeException();
            }
        }
    }
}
